package com.happypets.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.happypets.model.PetBean;
import com.happypets.services.PetService;
import com.happypets.servicesImpl.PetServiceImpl;

/**
 * Servlet implementation class IndexServlet
 */
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public IndexServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//进行相关处理
		String path = request.getContextPath();
		
		HttpSession session = request.getSession();
		
		session.setAttribute("indexFlag", "yes");
		
		PetService petService = new PetServiceImpl();
		
		//获取所有狗
		List<PetBean> dogs = petService.bigCategoryPets("1",4);
		//所有猫
		List<PetBean> cats = petService.bigCategoryPets("2",4);
		//所有鸟
		List<PetBean> birds = petService.bigCategoryPets("3",4);
		
		List<PetBean> twoShowPets  = new ArrayList<PetBean>();
		
		//添加两条到显示头部两个
		twoShowPets.add(dogs.get(0));
		twoShowPets.add(cats.get(0));
		
//		HttpServletRequest req = (HttpServletRequest)request;
//		HttpServletResponse res = (HttpServletResponse)response;
		session.setAttribute("petBeanes", twoShowPets);
		session.setAttribute("dogs", dogs);
		session.setAttribute("cats", cats);
		session.setAttribute("birds", birds);
		response.sendRedirect(path + "/web/index.jsp");
//		request.getRequestDispatcher("/web/index.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
